use std::{sync::atomic::AtomicBool, time::Instant};
use xcap::Monitor;

static STATE: AtomicBool = AtomicBool::new(false);

#[flutter_rust_bridge::frb(sync)]
pub fn get_state() {
    STATE.load(std::sync::atomic::Ordering::SeqCst);
}


#[flutter_rust_bridge::frb(sync)]
pub fn up_state(state: bool) {
    STATE.store(state, std::sync::atomic::Ordering::SeqCst);
}


#[flutter_rust_bridge::frb(sync)]
pub fn get_screen_info() -> (i32, u32, u32) {
    let monitors = Monitor::all().unwrap();
    let mut width = 0;
    let mut height = 0;
    //let mut x = 0;
    let mut y = 0;
    for monitor in monitors {
        width += monitor.width().unwrap();
        height += monitor.height().unwrap();
        //x += monitor.x().unwrap();
        y += monitor.y().unwrap();
        println!("x: {}, y: {}", monitor.x().unwrap(), monitor.y().unwrap());
    }
    println!("width: {}, height: {}", width, height);
    (y, width, height)
}

#[flutter_rust_bridge::frb(sync)]
pub fn screen_shot() -> Vec<(i32, i32, u32, u32, Vec<u8>)> {
    let start = Instant::now();
    let monitors = Monitor::all().unwrap();

    let mut list = vec![];
    for monitor in monitors {
        let image = monitor.capture_image().unwrap();
        let x = monitor.x().unwrap();
        let y = monitor.y().unwrap();
        let w = image.width();
        let h = image.height();
        let data = image.into_raw();
        list.push((x, y, w, h, data));
    }
    list.reverse();  
    println!("运行耗时: {:?}", start.elapsed());
    list
}